package com.syxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.service.impl.CrudServiceImpl;
import com.syxd.common.utils.BeanHelper;
import com.syxd.dao.SysJiangCreditContactDao;
import com.syxd.dao.SysJiangCreditDao;
import com.syxd.dto.SysJiangCreditDto;
import com.syxd.entity.SysJiangCreditContactEntity;
import com.syxd.entity.SysJiangCreditEntity;
import com.syxd.service.SysJiangAuditService;
import com.syxd.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysJiangAuditServiceImpl extends CrudServiceImpl<SysJiangCreditDao, SysJiangCreditEntity, SysJiangCreditDto> implements SysJiangAuditService {

    @Autowired
    SysJiangCreditContactDao sysJiangCreditContactDao;
    @Autowired
    SysUserService sysUserService;

    @Override
    public QueryWrapper<SysJiangCreditEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<SysJiangCreditEntity> wrapper = new QueryWrapper<>();
        String branchName = (String) params.get("branchName");
        String householderIdcard = (String) params.get("householderIdcard");
        String householderPhone = (String) params.get("householderPhone");
        String spouseName = (String) params.get("spouseName");
        String spouseIdcard = (String) params.get("spouseIdcard");
        String spousePhone = (String) params.get("spousePhone");
        String householderName = (String) params.get("householderName");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        if (StringUtils.isNotBlank(endDate)) {
            endDate = endDate + " 23:59:59";
        }


        wrapper.eq(StringUtils.isNotBlank(branchName), "branch_name", branchName);
        wrapper.eq(StringUtils.isNotBlank(householderIdcard), "householder_idcard", householderIdcard);
        wrapper.eq(StringUtils.isNotBlank(householderPhone), "householder_phone", householderPhone);
        wrapper.eq(StringUtils.isNotBlank(spouseName), "spouse_name", spouseName);
        wrapper.eq(StringUtils.isNotBlank(spouseIdcard), "spouse_idcard", spouseIdcard);
        wrapper.eq(StringUtils.isNotBlank(spousePhone), "spouse_phone", spousePhone);
        wrapper.eq(StringUtils.isNotBlank(householderName), "householder_name", householderName);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_time", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_time", endDate);
        wrapper.eq("is_delete", Constant.FDELETE);
    /*    if (params.get("activityIds") != null) {
            String activityIds = (String) params.get("activityIds");
            String[] split = activityIds.split(",");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                wrapper.like("activity_id", s);
                if (split.length - 1 != i) {
                    wrapper.or();
                }
            }
        }*/
        return wrapper;
    }


    @Override
    public WebResult deleteJiangCreditById(Long id, HttpServletRequest request) {

        SysJiangCreditContactEntity sysJiangCreditContactEntity = sysJiangCreditContactDao.selectById(id);
        SysJiangCreditEntity sysJiangCreditEntity = new SysJiangCreditEntity();
        sysJiangCreditEntity.setId(sysJiangCreditContactEntity.getSourceDataId().longValue());
        sysJiangCreditEntity.setIsDelete(Constant.DELETE);
        baseDao.updateById(sysJiangCreditEntity);
        sysJiangCreditContactEntity.setUpdateTime(new Date());
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysJiangCreditContactEntity.setApproverId(userId);
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int i = sysJiangCreditContactDao.updateById(sysJiangCreditContactEntity);
        if (i > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    /**
     * 新增
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    public WebResult add(Long id, HttpServletRequest request) {
        SysJiangCreditContactEntity sysJiangCreditContactEntity = sysJiangCreditContactDao.selectById(id);
        sysJiangCreditContactEntity.setId(null);
        SysJiangCreditEntity sysJiangCreditEntity = BeanHelper.copyProperties(sysJiangCreditContactEntity, SysJiangCreditEntity.class);
        int insert = baseDao.insert(sysJiangCreditEntity);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysJiangCreditContactEntity.setId(id);
        sysJiangCreditContactEntity.setApproverId(userId);
        sysJiangCreditContactEntity.setUpdateTime(new Date());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int update = sysJiangCreditContactDao.updateById(sysJiangCreditContactEntity);
        if (insert > 0 && update > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    /**
     * 修改
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    public WebResult put(Integer id, HttpServletRequest request) {
        SysJiangCreditContactEntity sysJiangCreditContactEntity = sysJiangCreditContactDao.selectById(id);
        SysJiangCreditEntity sysJiangCreditEntity = BeanHelper.copyProperties(sysJiangCreditContactEntity, SysJiangCreditEntity.class);
        sysJiangCreditEntity.setId(sysJiangCreditContactEntity.getSourceDataId().longValue());
        sysJiangCreditEntity.setLastupdateTime(new Date());
        int res = baseDao.updateById(sysJiangCreditEntity);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysJiangCreditContactEntity.setApproverId(userId);
        sysJiangCreditContactEntity.setUpdateTime(new Date());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int update = sysJiangCreditContactDao.updateById(sysJiangCreditContactEntity);
        if (res > 0 && update > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    @Override
    public WebResult refuse(Integer id, HttpServletRequest request) {
        SysJiangCreditContactEntity sysJiangCreditContactEntity = sysJiangCreditContactDao.selectById(id);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysJiangCreditContactEntity.setApproverId(userId);
        sysJiangCreditContactEntity.setUpdateTime(new Date());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.reject.getValue());
        int update = sysJiangCreditContactDao.updateById(sysJiangCreditContactEntity);
        if (update > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }
}
